home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_11.lha
/
6_11
/
6_11_cd.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
2KB
|
98 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*
Constructor and assignment of doubles
/
include <math.h>
include <limits.h>
include <float.h>
include <arbint.h>
include <swap.h>
*
Fill in an arep with a double.
Convert the double into a string
of ARB_type digits.
/
tatic void dassign(arep *p, double n)
p->refcnt = 1;
/* declare working storage for dtoarb */
define BITS(type) (CHAR_BIT * (int)sizeof(type))
const int dlen = DBL_MAX_EXP / BITS(ARB_type);
ARB_type s[dlen+1];
/* convert the double to positivt */
int neg = 0;
if (n < 0)
{
n = -n;
neg = 1;
}
// convert the double
double i = n;
ARB_type *s1 = (s + neg); // run forward on digits
// this leaves the digits in reverse order
do {
*s1++ = (ARB_type) fmod(i, ARB_base);
i /= ARB_base;
} while (i >= 1.0);
// run forward and backward on
// the digits reversing them
p->length = s1-- - (s + neg);
ARB_type *s2 = s;
while (s2 < s1)
swap(*s2++, *s1--);
// if necessary, convert to negative
if (neg)
{
ARB_Ltype k = 1;
s[0] = 0;
p->length++;
for (int j = p->length - 1; ; )
{
ARB_type l1 = ~s[j];
ARB_Ltype l = l1 + k;
s[j] = l;
if (--j <= 0)
break;
k = (l / ARB_base) ? 1 : 0;
}
}
// copy to allocated space
p->value = new ARB_type[p->length];
memcpy((char *)p->value, (char *)s,
p->length * sizeof(ARB_type));
* create an arbint from a double */
rbint::arbint(double n) // arbint x = 36.0;
p = new arep;
dassign(p, n);
* assign a double to an arbint */
rbint& arbint::operator=(double n) // x = 36.0;
if (p->refcnt > 1)
{
p->refcnt--;
p = new arep;
}
else
delete p->value;
dassign(p, n);
return *this;